const WebServerAccessKey = 'bb6bd652af8d247d1cd0e9a07359c08d'
const axios = require('axios')
const GetPositionAdcode = async (req, res) => {
  const { lat, lng } = req.body;
  if(lat && lng){
      const result  = await axios.get(`https://restapi.amap.com/v3/geocode/regeo`,{
    params:{
        key:WebServerAccessKey,
        location:`${lng.toFixed(6)},${lat.toFixed(6)}`,
        output:'json',
        radius: 1000,
        extensions: 'all'
    }
  })
  if(result.data.status == 1){    
    res.send({
    province:result.data.regeocode.addressComponent.province,
    city:result.data.regeocode.addressComponent.city,
    adcode:result.data.regeocode.addressComponent.adcode,
    district:result.data.regeocode.addressComponent.district,
    street:result.data.regeocode.addressComponent.streetNumber.street,
    township:result.data.regeocode.addressComponent.township,
    cityCode:result.data.regeocode.addressComponent.citycode,
  })
  }
  else{
    res.send({
    code:400,
    data:result.data,
    message:'获取位置失败'
    })
  }
  }
  else{
    res.send({
      code:400,
      message:'未获取到对应的经纬度'
    })
  }
};
module.exports = GetPositionAdcode;